//
//  OpenlimitsVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/15.
//  Copyright © 2020 tym. All rights reserved.
//

#import "OpenlimitsVC.h"
#import "UnlockingWayVC.h"
#import "ShowAlertView.h"
#import "AddUserVC.h"
#import "BangFamilyUserVC.h"
#import "SeleFamilyUserVC.h"
#import "UIImageView+YPImage.h"
#import "EquipmentService.h"
@interface OpenlimitsVC ()

@property(nonatomic,strong) UserModel *userModel;
@property(nonatomic,assign) NSInteger userId;
@property(nonatomic,strong) EquipmentModel *equipMent;

@property(nonatomic,strong) EquipmentService *services;

@property(nonatomic,weak) IBOutlet UIView *bangdingView;
@property(nonatomic,weak) IBOutlet NSLayoutConstraint *topH;

@property(nonatomic,weak) IBOutlet UIImageView *iamgeView;
@property(nonatomic,weak) IBOutlet UILabel *nickLab;
@property(nonatomic,weak) IBOutlet UILabel *bangLab;
@property(nonatomic,weak) IBOutlet UILabel *passLab;
@property(nonatomic,weak) IBOutlet UILabel *fingLab;
@property(nonatomic,weak) IBOutlet UILabel *cardLab;
@property(nonatomic,weak) IBOutlet UILabel *remateConLab;

@property(nonatomic,weak) IBOutlet UIButton *deleteBtn;
@end

@implementation OpenlimitsVC

-(instancetype)initWithUserModel:(UserModel*)userModel equipMent:(EquipmentModel*)equipMent{
    self = [super init];
    
    self.userModel = userModel;
    self.equipMent = equipMent;
    return self;
}

-(instancetype)initWithUserId:(NSInteger)userId equipMent:(EquipmentModel*)equipMent{
    self = [super init];
    
    self.userId = userId;
    self.equipMent = equipMent;
    return self;
}

-(EquipmentService*)services{
    if (_services == nil) {
        _services = [[EquipmentService alloc] init];
    }
    return _services;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"开锁权限"];
    //self.tz_interactivePopDisabled = YES;
    self.iamgeView.layer.masksToBounds = YES;
    self.iamgeView.layer.cornerRadius = 15.5;
    
    self.deleteBtn.layer.cornerRadius = 5;
    self.deleteBtn.layer.masksToBounds = YES;
    self.deleteBtn.layer.borderColor = rgba(151, 151, 151).CGColor;
    self.deleteBtn.layer.borderWidth = 1;
    
    [self updateUserInfo];
}

-(void)backViewFunc:(id)btn{
    if ([self.navigationController.viewControllers[self.navigationController.viewControllers.count-2] isKindOfClass:[AddUserVC class]]) {
        [self.navigationController popToViewController:self.navigationController.viewControllers[self.navigationController.viewControllers.count-3] animated:YES];
    }else{
        [super backViewFunc:btn];
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getUserDetaile];
}

//更新用户信息
-(void)updateUserInfo{
    if (_userModel) {
        if(_userModel.nickname){
            self.nickLab.text = _userModel.nickname;
        }
        if (_userModel.headImgUrl.length) {
            [self.iamgeView yp_setImageUlr:_userModel.getEquipmentImage];
        }
        if (self.userModel.passwordCount) {
            self.passLab.text = [NSString stringWithFormat:@"%ld个",self.userModel.passwordCount];
        }else{
            self.passLab.text = @"未设置";
        }
        if (self.userModel.fingerprintCount) {
            self.fingLab.text = [NSString stringWithFormat:@"%ld个",self.userModel.fingerprintCount];
        }else{
            self.fingLab.text = @"未设置";
        }
        if (self.userModel.cardCount) {
            self.cardLab.text = [NSString stringWithFormat:@"%ld个",self.userModel.cardCount];
        }else{
            self.cardLab.text = @"未设置";
        }
        
        if (self.userModel.remoteControlCount) {
            self.remateConLab.text = [NSString stringWithFormat:@"%ld个",self.userModel.remoteControlCount];
        }else{
            self.remateConLab.text = @"未设置";
        }
        
        if(self.userModel.type == 1){ //绑定app用户
            self.bangdingView.hidden = YES;
            self.topH.constant = 197-46;
        }else{//新增用户
            self.bangdingView.hidden = NO;
            self.topH.constant = 197;
        }
    }
}

-(void)getUserDetaile{
    TYMWeak(self);
    [self.services getEquipmentuseruserdetail:_equipMent.equipmentId userId:_userModel.userId?_userModel.userId:self.userId type:_userModel.userId?_userModel.type:2 completion:^(LMJBaseResponse * _Nonnull respons, UserModel * _Nonnull usermodel) {
        if (respons.message) {
            [WHToast showMessage:respons.message duration:2 finishHandler:nil];
        }else{
            weakself.userModel = usermodel;
            [weakself updateUserInfo];
        }
    }];
}

-(IBAction)passwordWay:(id)sender{
    
    UnlockingWayVC *passVC = [[UnlockingWayVC alloc] initWithType:1 equipMent:self.equipMent userModel:self.userModel];
    [self.navigationController pushViewController:passVC animated:YES];
}

-(IBAction)fingerWayAction:(id)sender{
    
    UnlockingWayVC *passVC = [[UnlockingWayVC alloc] initWithType:2 equipMent:self.equipMent userModel:self.userModel];
    [self.navigationController pushViewController:passVC animated:YES];
}

-(IBAction)cardWayAction:(id)sender{
    
    UnlockingWayVC *passVC = [[UnlockingWayVC alloc] initWithType:3 equipMent:self.equipMent userModel:self.userModel];
    [self.navigationController pushViewController:passVC animated:YES];
}

-(IBAction)yaokongWayAction:(id)sender{
    
    UnlockingWayVC *passVC = [[UnlockingWayVC alloc] initWithType:4 equipMent:self.equipMent userModel:self.userModel];
    [self.navigationController pushViewController:passVC animated:YES];
}

-(IBAction)bangdingAction:(id)sender{
    
    BangFamilyUserVC *bangVC = [[BangFamilyUserVC alloc] init];
    bangVC.type = 1;
    bangVC.youkeModel = self.userModel;
    bangVC.equipmentID = self.equipMent.equipmentId;
    [self.navigationController pushViewController:bangVC animated:YES];
}

-(IBAction)deleUserAction:(id)sender{
    TYMWeak(self);
    [ShowAlertView showAlertViewTitle:@"提示" message:@"是否要同时删除该用户名下所有的开锁方式" actionBtnTitle:@"是"
                           hiddenCancel:NO
                           completion:^(NSInteger actionType) {
        [weakself deleteUser:actionType];
    }];
}

-(void)deleteUser:(BOOL)isDele{
    
    TYMWeak(self);
    [self.services getEquipmentEeleteequipmentuserbyuserId:self.userModel.userId type:self.userModel.type equipmentId:self.equipMent.equipmentId
                                                deleteEquipmentUser:isDele completion:^(LMJBaseResponse * _Nonnull respons) {
        
        if (respons.code == 0) {
            [WHToast showMessage:@"删除成功" duration:2 finishHandler:^{
                [weakself backViewFunc:nil];
            }];
        }else{
            [ShowAlertView showAlertViewTitle:@"提示" message:respons.message actionBtnTitle:@"确定" hiddenCancel:NO completion:^(NSInteger actionType) {
                if (respons.code == 2001) {
                    if (actionType) {
                        [weakself getVerifyAdminroleSectionisDele:isDele];
                    }
                }
            }];
        }
    }];
}

//验证管理员
-(void)getVerifyAdminroleSectionisDele:(BOOL)dele{
    TYMWeak(self);
    [self.services getVerifyadminroleEpumentId:self.equipMent.equipmentId completion:^(LMJBaseResponse * _Nonnull respons) {
        if (respons.code == 0) {
            [weakself deleteUser:dele];
        }else{
            [WHToast showMessage:respons.message duration:2 finishHandler:nil];
        }
    }];
}



@end
